package com.gsafety.libraryktx.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.view.View.OnClickListener
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.IntDef
import androidx.core.content.ContextCompat
import com.gsafety.libraryktx.R
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy

/**
 * @author : lj
 * @date : 2020/7/20 17:22
 */
class EmptyLayout @JvmOverloads constructor(context: Context,
                                            attrs: AttributeSet? = null,
                                            defStyleAttr: Int = 0) : LinearLayout(context,attrs,defStyleAttr) {

    var mOnRetryListener: OnRetryListener? = null
    var mEmptyStatus = STATUS_HIDE

    private var mTvError: TextView? = null

    init {
       initview(attrs)
    }

    private fun initview(attrs:AttributeSet?){
        val a = context.obtainStyledAttributes(attrs, R.styleable.common_EmptyLayout)
        val bgColor: Int
        bgColor = try {
            a.getColor(R.styleable.common_EmptyLayout_common_background_color, Color.WHITE)
        } finally {
            a.recycle()
        }
        val view = View.inflate(context, R.layout.common_empty_loading_layout, this)
        mTvError = view.findViewById(R.id.tv_net_error)
        mTvError?.let {
            setOnClickListener(OnClickListener { v: View? ->
                if (mOnRetryListener != null) {
                    mOnRetryListener?.let {
                        it.onRetry()
                    }
                }
            })
        }
        setBackgroundColor(bgColor)
        switchEmptyView()
    }

    /**
     * 切换视图
     */
    private fun switchEmptyView() {
        when (mEmptyStatus) {
            STATUS_NO_DATA -> {
                visibility = View.VISIBLE
                setEmptyMessage(R.string.common_empty_not_data_text)
                setEmptyIcon(R.mipmap.common_empty_not_data_icon)
            }
            STATUS_NO_NET -> {
                visibility = View.VISIBLE
                setEmptyMessage(R.string.common_empty_not_net_text)
                setEmptyIcon(R.mipmap.common_empty_not_network_icon)
            }
            STATUS_HIDE -> visibility = View.GONE
            else -> {
            }
        }
    }

    /**
     * 设置状态
     *
     * @param emptyStatus
     */
    fun setEmptyStatus(@EmptyLayout.EmptyStatus emptyStatus: Int) {
        mEmptyStatus = emptyStatus
        switchEmptyView()
    }

    /**
     * 获取状态
     * @return  状态
     */
    fun getEmptyStatus(): Int {
        return mEmptyStatus
    }

    /**
     * 设置异常消息
     *
     * @param msg 显示消息
     */
    fun setEmptyMessage(msg: String?) {
        mTvError!!.text = msg
    }

    /**
     * 设置异常消息
     *
     * @param resId 显示消息
     */
    fun setEmptyMessage(resId: Int) {
        mTvError!!.setText(resId)
    }

    /**
     * 隐藏图标
     */
    fun hideErrorIcon() {
        mTvError!!.setCompoundDrawables(null, null, null, null)
    }

    /**
     * 设置图标
     * @param resId 资源ID
     */
    fun setEmptyIcon(resId: Int) {
        setEmptyIcon(ContextCompat.getDrawable(context, resId))
    }

    /**
     * 设置图标
     * @param drawable drawable
     */
    fun setEmptyIcon(drawable: Drawable?) {
        mTvError!!.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null)
    }

    /**
     * 设置重试监听器
     *
     * @param retryListener 监听器
     */
    fun setRetryListener(retryListener: OnRetryListener?) {
        mOnRetryListener = retryListener
    }

    /**
     * 点击重试监听器
     */
    interface OnRetryListener {
        fun onRetry()
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(STATUS_HIDE, STATUS_NO_NET, STATUS_NO_DATA)
    annotation class EmptyStatus
    companion object {
        const val STATUS_HIDE = 1001
        const val STATUS_NO_NET = 2
        const val STATUS_NO_DATA = 3
    }
}